package com.cyanite.playlv2.widget;

import com.cyanite.playlv2.util.StringFormatters;
import com.cyanite.playlv2.util.VenueUtils;
import com.cyanite.playlv2api.types.VenueCategory;
import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.types.Venue;
import com.cyanite.playlv2.R;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class VenueListAdapter extends BaseVenueAdapter {

    private LayoutInflater mInflater;
    private Context mContext;

    public VenueListAdapter(Context context) {
        super(context);
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.venue_list_item, null);

            // Creates a ViewHolder and store references to the two children
            // views we want to bind data to.
            holder = new ViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            holder.venueName = (TextView) convertView.findViewById(R.id.venueName);
            holder.locationLine1 = (TextView) convertView.findViewById(R.id.venueLocationLine1);
            holder.iconSpecial = (ImageView) convertView.findViewById(R.id.iconSpecialHere);
            holder.venueDistance = (TextView) convertView.findViewById(R.id.venueDistance);
            holder.iconTrending = (ImageView) convertView.findViewById(R.id.iconTrending);
            holder.venueCheckinCount = (TextView) convertView.findViewById(R.id.venueCheckinCount);
            holder.todoHere = (ImageView) convertView.findViewById(R.id.venueTodoCorner);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Venue venue = (Venue) getItem(position);
        VenueCategory category = venue.getCategory();
        if ( category != null ) {
            holder.icon.setImageResource(category.getIconVenueRes());
        } else {
            holder.icon.setImageResource(R.drawable.venue_category_none);
        }
        holder.venueName.setText(venue.getName());
        if (!TextUtils.isEmpty(venue.getAddress())) {
            holder.locationLine1.setText(StringFormatters.getVenueLocationFull(venue));
        } else {
            holder.locationLine1.setText("");
        }

        // If there's a special here, show the special here icon.
        if (VenueUtils.getSpecialHere(venue)) {
            holder.iconSpecial.setVisibility(View.VISIBLE);
        } else {
            holder.iconSpecial.setVisibility(View.GONE);
        }

        if (venue.getDistance() != null) {
            holder.venueDistance.setText(venue.getDistance()
                    + mContext.getResources().getString(R.string.nearby_venue_activity_meter));
        } else {
            holder.venueDistance.setText("");
        }
        // If more than two people here, then show trending text.
        if (venue.getCheckInsCountMoment() > 0) {
            holder.iconTrending.setVisibility(View.VISIBLE);
            holder.venueCheckinCount.setVisibility(View.VISIBLE);
            holder.venueCheckinCount.setText(venue.getCheckInsCountMoment()
                    + mContext.getResources().getString(R.string.nearby_venue_activity_herenow));
        }   else {
            holder.iconTrending.setVisibility(View.GONE);
            holder.venueCheckinCount.setVisibility(View.GONE);
        }

        // If we have a todo here, show the corner folded over.
        if (venue.getHasTodo()) {
        	holder.todoHere.setVisibility(View.VISIBLE);
        } else {
        	holder.todoHere.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    @Override
    public void setGroup(Group<Venue> g) {
        super.setGroup(g);
    }

    private static class ViewHolder {
        ImageView icon;
        TextView venueName;
        TextView locationLine1;
        ImageView iconSpecial;
        TextView venueDistance;
        ImageView iconTrending;
        TextView venueCheckinCount;
        ImageView todoHere;
    }
}
